import axios from 'axios';
import QS from 'qs';


// 公共参数
var gHost="https://plaza.juke1.xyz/";

axios.defaults.timeout = 15000;
axios.defaults.baseURL = gHost;
axios.defaults.withCredentials = true; //标识

//添加请求拦截器
axios.interceptors.request.use(function (config) {
  // store.commit('updLoading',true);
  return config;
}, function (error) {
  return Promise.reject(error);
});


// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // store.commit('updLoading',false);
  //登录过期处理
  return response;
}, function (error) {
  return Promise.reject(error);
});


/**
 * 封装get方法
 * @param {*} url 
 * @param {*} params 
 */
export function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, { params: params })
      .then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err);
      })
  })
}

/**
 * 封装postForm请求
 * @param {*} url 
 * @param {*} data 
 */
export function post(url, data = {},) {
  return new Promise((resolve, reject) => {
      var headers = {};
      if (Object.prototype.toString.call(data) === '[object FormData]') { //判断是formData类型
        headers = {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'};
      } else {
        headers = {'Content-Type': 'application/json'};
      }
      axios.post(
        url, 
        data,
        {
          headers:headers
        }
      )
        .then(response => {
          resolve(response.data)
        }, err => {
          //请求出错后跳转404
          reject(err);
        })
  })

}

